package com.stylefeng.guns.modular.api.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.api.service.IAwardsService;
import com.stylefeng.guns.modular.platform.model.Awards;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-07-25 20:09:05
 */
@Controller
@RequestMapping("/awards")
public class AwardsController extends BaseController {

    private String PREFIX = "/api/awards/";

    @Autowired
    private IAwardsService awardsService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "awards.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/awards_add")
    public String awardsAdd() {
        return PREFIX + "awards_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/awards_update/{awardsId}")
    public String awardsUpdate(@PathVariable Integer awardsId, Model model) {
        Awards awards = awardsService.selectById(awardsId);
        model.addAttribute("item",awards);
        LogObjectHolder.me().set(awards);
        return PREFIX + "awards_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return awardsService.selectList(null);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Awards awards) {
    	double sumRate  = awardsService.selectCountRateByActivityId(awards.getActivityId(), null);
    	if(sumRate + awards.getWinRate() >1){
    		return "奖项总中奖概率不能大于1";
    	}
    	awards.setCreateBy("1");
    	awards.setGmtCreate(new Date());
    	awards.setLastUpdatedBy("1");
    	awards.setGmtLastUpdate(new Date());
        awardsService.insert(awards);
        return SUCCESS;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer awardsId) {
        awardsService.deleteById(awardsId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Awards awards) {
    	double sumRate  = awardsService.selectCountRateByActivityId(awards.getActivityId(), awards.getId());
    	if(sumRate + awards.getWinRate() >1){
    		return "奖项总中奖概率不能大于1";
    	}
        awardsService.updateById(awards);
        return SUCCESS;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{awardsId}")
    @ResponseBody
    public Object detail(@PathVariable("awardsId") Integer awardsId) {
        return awardsService.selectById(awardsId);
    }
}
